package cn.ustb.serviceImpl;

import cn.ustb.dao.LevelExamListDao;
import cn.ustb.entity.LevelExamList;
import cn.ustb.entity.User;
import cn.ustb.service.LevelExamListService;
import cn.ustb.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author yuyan
 */
public class LevelExamListServiceImpl implements LevelExamListService {
    @Override
    public LevelExamList get(LevelExamList levelExamList) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MyBatisUtil.getInstance();
            LevelExamListDao levelExamListDao = sqlSession.getMapper(LevelExamListDao.class);
            return levelExamListDao.getByStudentIdandCourseId(levelExamList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public void insert(LevelExamList levelExamList) {
        SqlSession sqlSession=null;
        try{
            sqlSession = MyBatisUtil.getInstance();
            LevelExamListDao levelExamListDao = sqlSession.getMapper(LevelExamListDao.class);
            levelExamListDao.insert(levelExamList);

            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
    }

    @Override
    public List<LevelExamList> getByUser(User user) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MyBatisUtil.getInstance();
            LevelExamListDao levelExamListDao = sqlSession.getMapper(LevelExamListDao.class);
            return levelExamListDao.getByUser(user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public int isPay(LevelExamList levelExamList) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MyBatisUtil.getInstance();
            LevelExamListDao levelExamListDao = sqlSession.getMapper(LevelExamListDao.class);
            return levelExamListDao.isPay(levelExamList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return 0;
    }

    @Override
    public void updatePayStatus(int i, String courseId) {
        SqlSession sqlSession=null;
        try{
            sqlSession = MyBatisUtil.getInstance();
            LevelExamListDao levelExamListDao = sqlSession.getMapper(LevelExamListDao.class);
            levelExamListDao.updatePayStatus(i, courseId);

            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
    }
}
